﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using System;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using System.Threading;
using System.Collections.Concurrent;

namespace UtilPublic.Mysql
{
    /// <summary>
    /// 数据库执行线程
    /// </summary>
    public static class DbMysqlSecond
    {
        // 心跳开始时间
        private static long TicksCur = 0;
        // 心跳计时
        private static long TicksHour = 0;

        /// <summary>
        /// 数据库线程数据库连接
        /// </summary>
        public static DbMysql dbMysqlSecond = null;

        /// <summary>
        /// 启动发送消息线程
        /// </summary>
        public static void ThreadDbMysqlSecondStart(string DataSource, string Port, string UserId, string Password, string Database)
        {
            #region 连接到Mysql数据库

            try
            {
                dbMysqlSecond = new DbMysql(DataSource, Port, UserId, Password, Database);
                dbMysqlSecond.DbMysqlConnect();
            }
            catch (Exception ex)
            {
                loger.Fatal("数据库初始化失败", ex);
            }

            #endregion 连接到Mysql数据库

            while (true)
            {
                TicksCur = DateTime.Now.Ticks;
                //小时心跳
                if (TicksHour < TicksCur)
                {
                    LoopHour();
                    TicksHour = TicksCur + TimeSpan.TicksPerHour;
                }

                try
                {
                    RunAll();
                }
                catch (Exception ex)
                {
                    loger.Fatal("指令线程执行失败", ex);
                }
                Thread.Sleep(100);
            }
        }

        /// <summary>
        /// 小时心跳
        /// </summary>
        public static void LoopHour()
        {
            dbMysqlSecond.LoopExecute();
        }

        /// <summary>
        /// 待处理操作
        /// </summary>
        public static ConcurrentQueue<string> ListCmd = new ConcurrentQueue<string>();

        /// <summary>
        /// 添加操作
        /// </summary>
        /// <param name="cmd"></param>
        public static void AddCmd(string cmd)
        {
            ListCmd.Enqueue(cmd);
        }

        /// <summary>
        /// 处理操作
        /// </summary>
        public static void RunAll()
        {
            dbMysqlSecond.TryReConnect();
            while (ListCmd.TryDequeue(out var cmd))
            {
                dbMysqlSecond.RunCommand(cmd, true);
            }
        }
    }
    

}